<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
      <el-breadcrumb-item>科目管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="10">
      <!--搜索框-->
      <el-col :span="6">
        <el-input placeholder="按科目名称查询" v-model="search" class="input-with-select" @keyup.enter.native="searchData1">
          <el-button slot="append" icon="el-icon-search" @click="searchdata"></el-button>
        </el-input>
      </el-col>
      <!--添加课程的按钮-->
      <el-col :span="6">
        <el-button type="primary" @click="addcoursedialog = true" style="margin-left: 120px;">添加课程</el-button>
      </el-col>
    </el-row>

    <!--表格模块-->
    <el-table :data="courses"highlight-current-row
              ref='table'   border stripe height="300" style="margin-top: 30px;">
      <el-table-column type="index" fixed align="center"></el-table-column>
      <el-table-column prop="couId" label="课程号"  align="center" width="120"></el-table-column>
      <el-table-column prop="couName" label="课程名称" align="center" width="120"></el-table-column>
      <el-table-column prop="couTeacher" label="任课教师" align="center"></el-table-column>
      <el-table-column prop="couCredit" label="课程学分" align="center"></el-table-column>
      <el-table-column prop="week" label="起始周" align="center"></el-table-column>
      <el-table-column prop="couNature" label="课程性质" align="center" width="130"></el-table-column>
      <el-table-column prop="location" label="上课地点" align="center"></el-table-column>
      <el-table-column prop="couCollege" label="开课学院" align="center" width="150"></el-table-column>
      <el-table-column label="操作" fixed="right" width="120" align="center">
        <template slot-scope="scope">
          <!-- 修改 -->
          <el-tag
            type="primary"
            effect="dark"
            @click="openupdate(scope.row)"
          >编辑
          </el-tag>
          <!-- 删除 -->
          <el-tag
            type="danger"
            effect="dark"
            @click="deleteCourse(scope.row.couId)"
          >删除
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!--分页插件-->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="curpage"
      :page-sizes="[2, 4, 8, 10]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next"
      :total="total">
    </el-pagination>

    <!-- 弹出的添加模态框 -->
    <el-dialog title="添加课程" :visible.sync="addcoursedialog" @close="clearuseradd">
      <el-form :model="addcourseform"  ref="addform" label-width="auto"  style="width:93%"
               class="demo-form-inline form">
        <el-form-item  label="课程号" >
          <el-input v-model="addcourseform.couId" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="课程名称" >
          <el-input v-model="addcourseform.couName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="任课教师" >
          <el-input v-model="addcourseform.couTeacher" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="课程学分" >
          <el-input v-model="addcourseform.couCredit" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="起始周" >
          <el-input v-model="addcourseform.week" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="课程性质" >
          <el-select v-model="addcourseform.couNature"  filterable style="width: 100%;">
            <el-option label="必修课" value="必修课"></el-option>
            <el-option label="通识任选课" value="通识任选课"></el-option>
            <el-option label="选修课" value="选修课"></el-option>
            <el-option label="必修项目" value="必修项目"></el-option>
            <el-option label="英语拓展课" value="英语拓展课"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="上课地点" >
          <el-input v-model="addcourseform.location" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="开课学院" >
          <el-input v-model="addcourseform.couCollege" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addcoursedialog = false">取 消</el-button>
        <el-button type="primary" @click="addCourse">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑 -->
    <el-dialog title="编辑课程" :visible.sync="updatecoursedialog" @close="closeupdate">
      <el-form :model="updatecourseform"  ref="updateform" label-width="auto"  style="width:93%"
               class="demo-form-inline form">
        <el-form-item  label="课程号" >
          <el-input v-model="updatecourseform.couId" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="课程名称" >
          <el-input v-model="updatecourseform.couName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="任课教师" >
          <el-input v-model="updatecourseform.couTeacher" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="课程学分" >
          <el-input v-model="updatecourseform.couCredit" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="起始周" >
          <el-input v-model="updatecourseform.week" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="课程性质" >
          <el-input v-model="updatecourseform.couNature" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="上课地点" >
          <el-input v-model="updatecourseform.location" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="开课学院" >
          <el-input v-model="updatecourseform.couCollege" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updatecoursedialog = false">取 消</el-button>
        <el-button type="primary" @click="updateCourse">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>

</template>

<script>
  import axios from 'axios';//axios
  import Qs from 'qs';//qs
  export default {
    name:"courseList",
    //绑定数据
    data() {
      return {
        courses:[],//注意使用表格时，pop属性对应的内容等价于后端数据传输的字段(键)
        curpage:1,//当前页
        pagesize:4,//每页显示条数
        total:0,//总条数
        addcoursedialog:false,//默认时关闭模态框的
        updatecoursedialog:false,
        addcourseform:{
          couId:"",
          couName:"",
          couTeacher:"",
          couCredit:"",
          week:"",
          couNature:"",
          location:"",
          couCollege:""
        },//form表单中的数据绑定
        updatecourseform:{//修改功能数据绑定
          couId:"",
          couName:"",
          couTeacher:"",
          couCredit:"",
          week:"",
          couNature:"",
          location:"",
          couCollege:""
        },
        search:"",//模糊查询的内容
      }
    },
    //文档加载事件,当没有任何按钮去点击触发事件时，可以使用该函数在加载时，触发事件！
    mounted() {
      this.courseList();
    },
    methods:{
      selectRow(row) { // 给操作行 高亮
        if (row) {
          this.$refs.table.setCurrentRow(row, true);  // 高亮某一行
        } else {
          this.$refs.table.setCurrentRow();  // 取消高亮
        }
      },
      //执行分页查询的函数
      courseList(curpage = 1) {
        var courses=this;
        axios.get(
          "http://localhost:1313/course/queryCourseByPage",
          {params:{current:courses.curpage, rows: courses.pagesize,search:courses.search}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.courses = res.data.data.courses;
          this.total = res.data.data.total;
        });
      },
      // 监听 pageSize 改变的事件
      handleSizeChange (newSize) {
        //   console.log(newSize)
        //  将监听接受到的每页显示多少条的数据 newSzie 赋值给 pagesize
        this.pagesize = newSize;
        //  修改完以后，重新发起请求获取一次数据
        this.courseList();
      },
      // 监听 页码值  改变的事件
      handleCurrentChange (newPage) {
        //   console.log(newPage)
        //  将监听接受到的页码值的数据 newPage 赋值给 pagenum
        this.curpage = newPage;
        //  修改完以后，重新发起请求获取一次数据
        this.courseList();
      },
      //添加课程
      addCourse() {
        var addInfo = this;
        //判断当前校验是否通过
        this.$refs.addform.validate((valid) => {
          if(valid) {
            //发送axios请求
            axios({
              headers:{'Content-Type':'application/x-www-form-urlencoded'},
              method:"post",
              url:"http://localhost:1313/course/courseInsert",
              data:Qs.stringify(addInfo.addcourseform)
            }).then((res)=>{
              console.log(res)
              if (res.data.code == 200) {
                //1.提示消息，是否添加成功
                this.$message({
                  message: res.data.msg,
                  type:"success",
                });
                //2.关闭模态框
                this.addcoursedialog=false;
                //3.刷新页面<==>再次执行courseList
                this.courseList();
              } else {
                this.$message.error(res.data.msg)
              }
            })
          }
        });
      },
      //关闭添加课程的模态框
      clearuseradd() {
        // 重置表单内容
        this.$refs.addform.resetFields();
      },
      openupdate(data){
        console.log(data)
        //打开模态框
        this.updatecoursedialog=true;
        //数据回显
        this.updatecourseform = data;
      },
      //修改课程
      updateCourse() {
        var updateInfo = this;
        axios({
          headers:{'Content-Type':'application/x-www-form-urlencoded'},
          method:"post",
          url:"http://localhost:1313/course/courseUpdate",
          data:Qs.stringify(updateInfo.updatecourseform)
        }).then((res)=>{
          if (res.data.code == 200) {
            this.$message({
              message:res.data.msg,
              type:"success"
            });
            this.updatecoursedialog = false;
            this.courseList();
          }else{
            this.$message.error(res.data.msg)
          }
        })
      },
      //关闭修改的模态框
      closeupdate() {
        this.$refs.updateform.resetFields();
      },
      //删除课程
      deleteCourse(data){
        console.log(data)
        //询问窗口
        this.$confirm("是否删除该课程？","提示",{
          confirmButtonText:"确定",
          cancelButtonText:"取消",
          type:"warning",
        }).then(()=>{
          //发送请求到后端
          axios.get(
            "http://localhost:1313/course/courseDelete",
            {params:{couId:data}}
          ).then((res)=>{
            if (res.data.code == 200) {
              this.$message({
                type:"success",
                message:res.data.msg
              });
              this.curpage = 1;//重新设置当前页面
              this.courseList();//刷新页面
            }else{
              this.$message.error(res.data.msg);
            }
          })
        }).catch(()=>{
          this.$message({
            type:"info",
            message:"已取消删除"
          })
        })
      },
      //模糊查询
      searchdata(){
        this.curpage = 1;
        //回调分页查询，增加分页查询的参数
        this.courseList();
        this.search ="";
      },
      // 按下回车键实现搜索
      searchData1(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.searchdata();
        }
      },
    }
  }

</script>

<style>
  /* 用来设置当前页面element全局table 选中某行时的背景色*/
  .el-table__body tr.current-row>td{
    background-color:  rgba(227, 189, 241, 0.76) !important;
    color: #fff;
  }
  .el-input--suffix .el-input__inner {
    padding-right: 15px;
  }
  .form{
    display: grid;
    justify-content: center;
    /* align-items: center; */
    justify-items: center;
  }
  .el-button--info.is-plain {
    color: #704141;
    background: #f4f4f5;
    border-color: #d3d4d6;
  }
  .el-button--mini {
    font-size: 14px;
    border-radius: 3px;
  }
  .el-breadcrumb{
    padding-bottom: 20px;
  }
  .el-pagination{
    padding-top: 20px;
  }
</style>
